<?php

class Login extends Controller {

    public function __construct() {
        parent::__construct();

        $this->view->js_desktop[0] = URL . 'public/js/jquery_min.js';
        $this->view->js_desktop[1] = URL . 'public/js/jquery_ui_min.js';
        
        $this->view->css_desktop[0] = URL . 'public/css/default.css';
        
        $this->view->css_mobile[0] = 'http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css';
        $this->view->js_mobile[0] = 'http://code.jquery.com/jquery-1.8.2.min.js';
        $this->view->js_mobile[1] = 'http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js';
        }

    public function index() {
            $this->view->render('mobile/login/index');
    }
    
    function addPlayer() {
        $username = $_GET["username"];
        //Add RatingScore for memberId
        $this->model->AddPassiveUser($username, 1000);
        
        $this->index();
        
    }
    
    
    function addMatch() {

        header("Location: ".URL."ratings");        
        
        $memberIdPlayer1TeamA = $_GET["selectTeamAPlayer1"];
        $memberIdPlayer2TeamA = $_GET["selectTeamAPlayer2"];
        $memberIdPlayer1TeamB = $_GET["selectTeamBPlayer1"];
        $memberIdPlayer2TeamB = $_GET["selectTeamBPlayer2"];
        $ScoreTeamA = $_GET["pointsTeamA"];
        $ScoreTeamB = $_GET["pointsTeamB"];
         
        $this->model->AddMatchResult($memberIdPlayer1TeamA, $memberIdPlayer2TeamA, $memberIdPlayer1TeamB, $memberIdPlayer2TeamB, $ScoreTeamA, $ScoreTeamB);
               
        $Player1TeamARating = $this->model->GetRating($memberIdPlayer1TeamA);
        $Player2TeamARating = $this->model->GetRating($memberIdPlayer2TeamA);
        $Player1TeamBRating = $this->model->GetRating($memberIdPlayer1TeamB);
        $Player2TeamBRating = $this->model->GetRating($memberIdPlayer2TeamB);
        
        $FactorK = 50;
        $ScoreMagnitude = 400;
        
        $RatingChange = $this->model->CalcRatingChange($Player1TeamARating, $Player2TeamARating, $Player1TeamBRating, $Player2TeamBRating, $ScoreTeamA, $ScoreTeamB, $FactorK, $ScoreMagnitude);
        
        $this->model->ChangeRating($memberIdPlayer1TeamA, $Player1TeamARating+$RatingChange) ;
        $this->model->ChangeRating($memberIdPlayer2TeamA, $Player2TeamARating+$RatingChange) ;
        $this->model->ChangeRating($memberIdPlayer1TeamB, $Player1TeamBRating-$RatingChange) ;
        $this->model->ChangeRating($memberIdPlayer2TeamB, $Player2TeamBRating-$RatingChange) ;
        

    }
}